package com.genius.reservation.common;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description= "返回响应数据")
public  class  ResponseRule<T> implements Serializable {

    @ApiModelProperty("状态码")
    int code;
    @ApiModelProperty("返回数据")
    T data;
    @ApiModelProperty("返回的信息")
    String msg;
    @ApiModelProperty("返回的时间戳")
    String timeStamp;


    private static ResponseRule response = new ResponseRule();
//    public static ResponseRule getInstance(){
//        response.setTimeStamp(DateTime.now().toString());
//        if (response == null) {
//            response = new ResponseRule<>();
//            response.setTimeStamp(DateTime.now().toString());
//        }
//        return response;
//    };


    public static <T> ResponseRule<T> ok(T data,String msg){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.OK.getCode());
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    public static <T> ResponseRule<T> ok(T data){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.OK.getCode());
        response.setMsg(ErrorCode.OK.getMsg());
        response.setData(data);
        return response;
    }

    public static ResponseRule ok(){

        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.OK.getCode());
        response.setMsg(ErrorCode.OK.getMsg());
        response.setData(null);
        return response;
    }

    public static ResponseRule ok(String msg){

        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.OK.getCode());
        response.setMsg(msg);
        response.setData(null);
        return response;
    }
    public static <T> ResponseRule<T> no(T data,String msg){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.NO.getCode());
        response.setMsg(msg);
        response.setData(data);
        return response;
    }
    public static <T> ResponseRule<T> no(T data){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.NO.getCode());
        response.setMsg(ErrorCode.NO.getMsg());
        response.setData(data);
        return response;
    }
    public static ResponseRule no(){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.NO.getCode());
        response.setMsg(ErrorCode.NO.getMsg());
        response.setData(null);
        return response;
    }

    public static ResponseRule no(String msg){
        ResponseRule response = new ResponseRule();
        response.setCode(ErrorCode.NO.getCode());
        response.setMsg(msg);
        response.setData(null);
        return response;
    }


    public String toJSONObject(){
        return JSONObject.toJSONString(this);
    }



}
